package com.xingchen.loveparttimejob.dao.impl;

import com.xingchen.loveparttimejob.dao.JobReleaseDAO;
import com.xingchen.loveparttimejob.model.pojo.JobInfo;
import com.xingchen.loveparttimejob.model.pojo.JobRelease;
import com.xingchen.loveparttimejob.model.pojo.User;
import com.xingchen.loveparttimejob.uitls.JDBCUtils;
import com.xingchen.loveparttimejob.uitls.PagerUtil;

import javax.security.auth.kerberos.KeyTab;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 小志
 * @date 2024/12/7 - 19:16
 */
public class JobReleaseDAOImpl implements JobReleaseDAO {
    @Override
    public ArrayList<JobRelease> getObjectData(JobRelease jobRelease, boolean closeDB) {
        return null;
    }

    @Override
    public Integer getDataCount(User user, boolean closeDB) {
        return null;
    }

    @Override
    public Integer addJobRelease(JobRelease jobRelease, boolean closeDB) {
        String sql = "";
        try {
            JDBCUtils.getInstance().executeQueryCountOrId(sql,new ArrayList<>(),closeDB);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public PagerUtil<JobRelease> getSelectJobReleaseList(PagerUtil<JobRelease> pager, String key, boolean closeDB) {

        List<Object> parameters = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select row_number() over(order by a.ji_id asc) as 序列,* from t_jobRelease as a join t_jobInfo as b on b.ji_id = a.ji_id where 1=1 ");

        if(pager.getSearch() != null && !Objects.equals(pager.getSearch(), "")){
            sql.append("and b.ji_title like ? ");
            parameters.add("%"+pager.getSearch()+"%");
        }

        if(key != null && !Objects.equals(key, "")){
            sql.append("and a.u_id = ? ");
            parameters.add(Integer.parseInt(key));
        }

        sql.append("order by a.ji_id OFFSET (?-1)*? rows fetch next ? rows only");
        parameters.add(pager.getIndexPage());
        parameters.add(pager.getShowRows());
        parameters.add(pager.getShowRows());

        try {
            ArrayList<JobRelease> objects = JDBCUtils.getInstance().executeQuery(String.valueOf(sql), parameters, new JobRelease().getMapping(), JobRelease.class, true);
            pager.setData(objects);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return pager;
    }


}
